package com.redoor.app.stake.basicdata.service.impl;

import com.redoor.app.stake.auth.commons.DatabaseServiceStatus;
import com.redoor.app.stake.auth.model.BaseUser;
import com.redoor.app.stake.auth.service.BaseUserService;
import com.redoor.app.stake.basicdata.dao.BaseChargerMapper;
import com.redoor.app.stake.basicdata.dao.BaseParkingAreaMapper;
import com.redoor.app.stake.basicdata.dao.BaseParkingSpaceMapper;
import com.redoor.app.stake.basicdata.model.BaseCharger;
import com.redoor.app.stake.basicdata.model.BaseParkingArea;
import com.redoor.app.stake.basicdata.model.BaseParkingSpace;
import com.redoor.app.stake.basicdata.model.BaseStation;
import com.redoor.app.stake.basicdata.model.controllerModel.BaseParkingAreaCriteria;
import com.redoor.app.stake.basicdata.model.serviceModel.BaseParkingAreaServiceBean;
import com.redoor.app.stake.basicdata.service.BaseParkingAreaService;
import com.redoor.app.stake.basicdata.service.BaseStationService;
import com.redoor.app.stake.common.utils.UUIDTool;
import com.redoornetwork.framework.core.exception.ServiceException;
import com.redoornetwork.framework.core.response.CoreStatus;
import com.redoornetwork.framework.mybatis.service.AbstractService;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * Created by wangjunlei on 2018-02-12 10:08:10.
 */
@Service
public class BaseParkingAreaServiceImpl extends AbstractService<BaseParkingArea, String> implements BaseParkingAreaService {

    @Autowired
    private BaseParkingAreaMapper baseParkingAreaMapper;
    @Autowired
    private BaseUserService baseUserService;
    @Autowired
    private BaseStationService baseStationService;
    @Autowired
    private BaseParkingSpaceMapper baseParkingSpaceMapper;
    @Autowired
    private BaseChargerMapper baseChargerMapper;
    @Override
    public List<BaseParkingArea> qryParkingAreaInfoList(BaseParkingAreaCriteria params) {
        return baseParkingAreaMapper.qryParkingAreaList(params);
    }

    /**
     * 查询停车区详细列表-停车区列表页面使用
     *
     * @param params
     * @Author zhangMumu
     * @Date 2018/3/13 19:48
     */
    @Override
    public List<BaseParkingAreaServiceBean> qryParkingAreaInfoListForPage(BaseParkingAreaCriteria params) {
        return baseParkingAreaMapper.qryParkingAreaListForPage(params);
    }
    /**
     * 查询停车区详细列表数量-页面使用
     * @Author zhangMumu
     * @Date 2018/3/13 19:48
     */
    @Override
    public int qryParkingAreaInfoListForPageCnt(BaseParkingAreaCriteria params) {
        return baseParkingAreaMapper.qryParkingAreaCnt(params);
    }

    @Override
    public List<BaseParkingArea> qryBaseParkingArea(BaseParkingAreaCriteria criteria) {
        return this.baseParkingAreaMapper.qryBaseParkingArea(criteria);
    }
    
    /**  
     *  新增或者修改停车区
     *   
     * @author ZhangMumu  
     * @date 2018/3/18 0018 23:39  
     * @param   
     * @return   
     */  
    @Override
    public BaseParkingArea saveParkarea(BaseParkingArea baseParkingArea) {
        //  传参为空,抛出异常
        if(null == baseParkingArea){
            throw new ServiceException(CoreStatus.SERVER_INTERNAL_ERROR);
        }
        BaseUser loginUser = this.baseUserService.getShiroUser(null);
        /*停车区uuid为空,新增一个停车区*/
        if(StringUtils.isBlank(baseParkingArea.getUuid())){
            /*若停车区所属站uuid为空,则无法生成停车区编码*/
            if (StringUtils.isBlank(baseParkingArea.getStationuuid())){
                throw new ServiceException(CoreStatus.SERVER_INTERNAL_ERROR);
            }

            baseParkingArea.setCode(this.genParkAreaCode(baseParkingArea.getStationuuid()));
            baseParkingArea.setParkidx( baseParkingArea.getCode());
            baseParkingArea.setUuid(UUIDTool.getUUID());
            baseParkingArea.setCreatedat(new Date());
            baseParkingArea.setCreatoruuid(loginUser.getUuid());
            baseParkingArea.setStatus(Integer.valueOf(DatabaseServiceStatus.SERVICE_NORMAL_STATUS.getCode()));
            /*保存成功*/
            if (baseParkingAreaMapper.insert(baseParkingArea) > 0){
                return baseParkingArea;
            }else {
                throw new ServiceException(CoreStatus.SERVER_INTERNAL_ERROR);
            }

        /*停车区编码不为空,跟新该停车区*/
        }else {
            BaseParkingArea oldParkArea = this.findById(baseParkingArea.getUuid());
            if (null == oldParkArea){
                throw new ServiceException(CoreStatus.SERVER_INTERNAL_ERROR);
            }else {
                baseParkingArea.setUpdatedat(new Date());
                baseParkingArea.setUpdateruuid(loginUser.getUuid());
                /*保存成功*/
                if (baseParkingAreaMapper.updateByPrimaryKeySelective(baseParkingArea) > 0){
                    return baseParkingArea;
                }else {
                    throw new ServiceException(CoreStatus.SERVER_INTERNAL_ERROR);
                }
            }
        }
    }

    @Override
    public int delParkarea(String uuid) {

        BaseUser loginUser = baseUserService.getShiroUser(null);
        BaseParkingArea baseParkingArea = this.findById(uuid);
        if (null != baseParkingArea){
            BaseParkingSpace baseParkingSpace = new BaseParkingSpace();
            baseParkingSpace.setStatus(Integer.valueOf(DatabaseServiceStatus.SERVICE_NORMAL_STATUS.getCode()));
            baseParkingSpace.setParkingareauuid(uuid);
            List<BaseParkingSpace> baseParkingSpaceList = this.baseParkingSpaceMapper.select(baseParkingSpace);
            //当停车区下有停车位的时候进行判断
            if(baseParkingSpaceList.size() >0) {
                //计数器 TODO
                int i = 0;
                for (BaseParkingSpace bean : baseParkingSpaceList) {
                    BaseCharger baseCharger = new BaseCharger();
                    baseCharger.setStatus(Integer.valueOf(DatabaseServiceStatus.SERVICE_NORMAL_STATUS.getCode()));
                    baseCharger.setSpaceuuid(baseCharger.getUuid());
                    //查询停车位绑定的枪
                    i += this.baseChargerMapper.selectCount(baseCharger);
                }
                //当停车位绑定有枪的时候返回整形状态0(未进行区的删除)
                if (i > 0) {
                    return 0;
                }
            }
            baseParkingArea.setUpdateruuid(loginUser.getUuid());
            baseParkingArea.setUpdatedat(new Date());
            baseParkingArea.setStatus(Integer.valueOf(DatabaseServiceStatus.SERVICE_DEL_STATUS.getCode()));
            baseParkingAreaMapper.updateByPrimaryKeySelective(baseParkingArea);
            this.baseParkingSpaceMapper.deleteSpaceByAreaUuid(uuid);
            return 1;
        }else {
            throw new ServiceException(CoreStatus.SERVER_INTERNAL_ERROR);
        }
    }

    @Override
    public BaseParkingAreaServiceBean parkareaDetail(String uuid) {
        if (StringUtils.isBlank(uuid)){
            throw new ServiceException(CoreStatus.SERVER_INTERNAL_ERROR);
        }
        return baseParkingAreaMapper.qryParkingAreaDetail(uuid);
    }

    @Override
    public String genParkAreaCode(String stationUuid) {

        /*通过充电站生成停车区编码*/
        class genCodeByStationNo{
            public String genCode(String stationUuid,int parkingAreaListSize){
                BaseStation station = baseStationService.findById(stationUuid);
                if (null == station){
                    throw new ServiceException(CoreStatus.SERVER_INTERNAL_ERROR);
                }

                /*停车区编码 = 充电站编码前五位 + 当前充电站的停车区数量 + 1*/
                return station.getStationno().substring(0,4) + (parkingAreaListSize + 1);
            }
        }

        if (StringUtils.isBlank(stationUuid)){
            throw new ServiceException(CoreStatus.SERVER_INTERNAL_ERROR);
        }
        
        List<String> parkingAreaCodesList = baseParkingAreaMapper.findParkAreaCodesByStationUuid(stationUuid);
        return new genCodeByStationNo().genCode(stationUuid,parkingAreaCodesList.size()) ;

//  注释掉的原因: 本打算查停车区编码然后拿到最大值直接递增生成新的编码,但是考虑到拿到的最大值可能不是按照上面的规则生成的,所以注释掉了.

//        /*若充电站下没有停车区,则通过站编码生成停车区编码*/
//        if (parkingAreaCodesList.isEmpty()){
//            return new genCodeByStationNo().genCode(stationUuid,parkingAreaCodesList.size()) ;
//        }else {
//            Collections.sort(parkingAreaCodesList);
//            /*取到最大的停车区编码*/
//            String maxParkingAreaCode = parkingAreaCodesList.get(parkingAreaCodesList.size()-1);
//            /*最大的停车区编码能转成Integer*/
//            if (NumberUtils.isCreatable(maxParkingAreaCode)){
//                return (Integer.parseInt(maxParkingAreaCode) + 1) + "";
//
//            /*最大停车区编码不能转成Integer*/
//            }else {
//                return new genCodeByStationNo().genCode(stationUuid,parkingAreaCodesList.size());
//            }
//
//        }

    }
}
